#pragma semicolon 1
#include <sourcemod>
#include <cstrike>
#include <sdkhooks>
#include <sdktools>
#include <colors>
#include <cstrike>
#include <PowerupsX>

#define PREFIX "{olive}PowerupsX {default}>{green} "
new AbilityMedic;
#define HEALAMOUNT 15

new bool:AbilityMedicUse[MAXPLAYERS+1];

new Float:MedicReturnTime = 5.0;
new Float:fThrowingForce = 60000000.0;

public Plugin:myinfo= 
{
	name="PowerupsXL Medic",
	author="Skydive",
	description="PowerupsX Medic Ability",
	version="1.0",
	url=""
};


public OnMapStart()
{
	PrecacheModel("models/healthvial.mdl",true);
}

public OnPluginStart()
{
	NativeIncStart();
	
	AbilityMedic = PX_RegisterAbility("AbilityMedic");
	PX_SetAbilityName(AbilityMedic,"Medic");
	PX_SetAbilityDescription(AbilityMedic,"Heal: Level*5 MaxHealth+: Level*10 Recharge: 1-Level/10.");
	PX_SetAbilityUsage(AbilityMedic,"Knife fires a Health Vial.");
	PX_AbilityUseColor(AbilityMedic,true);
	PX_SetAbilityColor(AbilityMedic,2,2,255,255);
	PX_SetAbilityMaxLevel(AbilityMedic,10);

	HookEvent("weapon_fire",Event_WeaponFire);
}

public PXF_OnAbilityGiven(Client, Ability)
{
	if(Ability == AbilityMedic)
	{
		AbilityMedicUse[Client] = true;
	}
}

public Action:Event_WeaponFire(Handle:event, const String:name[],bool:dontBroadcast)
{
	decl String:sWeapon[30];
	GetEventString(event,"weapon",sWeapon,30);
	new userid = GetClientOfUserId(GetEventInt(event,"userid"));
	new Level = PX_GetLevel(userid, PX_GetAbility(userid));
	
	if(PX_GetAbility(userid) == AbilityMedic && AbilityMedicUse[userid] == true)
	{
		if(StrEqual(sWeapon, "knife"))
		{	
			if(IsFakeClient(userid))
			{
				return;
			}
			
			new Float:fPlayerPos[3];
			new Float:fPlayerAngles[3];
			new Float:fThrowingVector[3];
			new Float:fDistanceFromSpawner = 40.0;
			
			GetClientEyeAngles( userid, fPlayerAngles );
			GetClientEyePosition( userid, fPlayerPos );
			
			TR_TraceRayFilter( fPlayerPos, fPlayerAngles, MASK_SOLID, RayType_Infinite, DontHitSelf, userid );
			
			if( TR_DidHit( ) )
			{
				new Float:fEndPosition[3];
				
				TR_GetEndPosition( fEndPosition );
				
				if( GetVectorDistance( fPlayerPos, fEndPosition ) < fDistanceFromSpawner )
				{
					return;
				}
			}
			
			EmitAmbientSound( "items/smallmedkit1.wav", fPlayerPos, userid ); 
			
			new Float:fLen = fDistanceFromSpawner * Sine( DegToRad( fPlayerAngles[0] + 90.0 ) );
			
			fPlayerPos[0] = fPlayerPos[0] + fLen * Cosine( DegToRad( fPlayerAngles[1] ) );
			fPlayerPos[1] = fPlayerPos[1] + fLen * Sine( DegToRad( fPlayerAngles[1] ) );
			fPlayerPos[2] = fPlayerPos[2] + fDistanceFromSpawner * Sine( DegToRad( -1 * fPlayerAngles[0] ) ) ;
			
			new entity = CreateEntityByName( "prop_physics_override" );
	
			DispatchKeyValue( entity, "model", "models/healthvial.mdl" );
			DispatchKeyValue( entity, "massScale", "1.5" );
			DispatchKeyValue( entity, "spawnflags", "6" );
			
			DispatchSpawn( entity );
			ActivateEntity( entity );
			
			new Float:fScal = fThrowingForce * Sine( DegToRad( fPlayerAngles[0] + 90.0 ) );
			
			fThrowingVector[0] = fScal * Cosine( DegToRad( fPlayerAngles[1] ) );
			fThrowingVector[1] = fScal * Sine( DegToRad( fPlayerAngles[1] ) );
			fThrowingVector[2] = fThrowingForce * Sine( DegToRad( -1 * fPlayerAngles[0] ) );
			
			TeleportEntity( entity, fPlayerPos, fPlayerAngles, fThrowingVector );
			SetEntProp(entity, Prop_Send, "m_CollisionGroup", 1);
			SetEntProp(entity, Prop_Send, "m_usSolidFlags", 8);
			
			
			SetEntPropEnt(entity, Prop_Send, "m_hOwnerEntity", userid);
			SDKHook(entity, SDKHook_TouchPost, StartMedTouchPost);
			
			AbilityMedicUse[userid] = false;
			
			CreateTimer( 20.0+Level*10, DestroyTimer, entity );
			CreateTimer( MedicReturnTime-Level/10, ReturnTimer, userid );
		}
	}
}

public Action:DestroyTimer( Handle:hTimer, any:edict )
{
	if(IsValidEntity(edict))
	{
		new String:dname[32], ent = CreateEntityByName("env_entity_dissolver");
		
		Format(dname, sizeof(dname), "dis_%d", edict);
		  
		if (ent > 0)
		{
			DispatchKeyValue(edict, "targetname", dname);
			DispatchKeyValue(ent, "dissolvetype", "3");
			DispatchKeyValue(ent, "target", dname);
			AcceptEntityInput(ent, "Dissolve");
			AcceptEntityInput(ent, "kill");
		}
	}
	return Plugin_Continue;
}

public Action:ReturnTimer( Handle:hTimer, any:Client )
{
	AbilityMedicUse[Client] = true;
	return Plugin_Continue;
}

public StartMedTouchPost(entity, other)
{
	if(other > 0 && other <= MaxClients)
	{
		new health = GetEntProp(other, Prop_Send, "m_iHealth");
		new Owner = GetEntPropEnt(entity, Prop_Send, "m_hOwnerEntity");
		new Level = PX_GetLevel(Owner, PX_GetAbility(Owner));
		new Heal = Level*5;
		new MaxHealth = 100+Level*10;
		if(health < MaxHealth)
		{
			if (health+Heal > MaxHealth)
			{
				SetEntProp(other,Prop_Send,"m_iHealth",MaxHealth);
				ClientCommand(other, "playgamesound items/smallmedkit1.wav");
				AcceptEntityInput(entity, "Kill");
				return;
			}
			else if(health == MaxHealth)
			{
				return;
			}
			
			SetEntProp(other,Prop_Send,"m_iHealth",health+Heal);
			ClientCommand(other, "playgamesound items/smallmedkit1.wav");
		}
		AcceptEntityInput(entity, "Kill");
	}
}

public bool:DontHitSelf( entity, mask, any:data )
{
	if( entity == data )
	{
		return false;
	}
	
	return true;
}